package org.dragonnova.meetingapp.app.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import org.dragonnova.meetingapp.R;
import org.dragonnova.meetingapp.app.fragment.main.MeFragment;
import org.dragonnova.meetingapp.app.fragment.main.MeetingFragment;
import org.dragonnova.meetingapp.app.fragment.main.NewsFragment;
import org.dragonnova.meetingapp.app.fragment.main.TranslationFragment;
import org.dragonnova.meetingapp.app.adapter.FragmentViewPagerAdapter;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    private FragmentManager mFragmentManager;
    private int mPreCheckedId = 0;
    private int mFragmentIndex = 0;

    private ViewPager main_vp_container;
    private RadioGroup main_rg_radio;
    private Toolbar mToolbar;
    private TextView main_tb_tv_title;
    private RelativeLayout main_tb_rl_title;

    private String[] mTabs = {"首页", "会议", "翻译", "我"};
    private ArrayList<Fragment> mFragmentList;
    private int[] mCheckIds = {R.id.main_rb_news, R.id.main_rb_meeting, R.id.main_rb_translation, R.id.main_rb_me};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
    }

    private void initView() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        //设置不显示默认标题
        if (getSupportActionBar() != null) getSupportActionBar().setDisplayShowTitleEnabled(false);

        main_tb_rl_title = (RelativeLayout) findViewById(R.id.main_tb_rl_title);
        main_tb_tv_title = (TextView) findViewById(R.id.main_tb_tv_title);
        main_vp_container = (ViewPager) findViewById(R.id.main_vp_container);
        main_rg_radio = (RadioGroup) findViewById(R.id.main_rg_radio);

        main_rg_radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (mPreCheckedId != checkedId) {
                    int index = 0;
                    switch (checkedId) {
                        case R.id.main_rb_news:
                            index = 0;
                            break;
                        case R.id.main_rb_meeting:
                            index = 1;
                            break;
                        case R.id.main_rb_translation:
                            index = 2;
                            break;
                        case R.id.main_rb_me:
                            index = 3;
                            break;
                    }
                    main_vp_container.setCurrentItem(index);
                    mPreCheckedId = mCheckIds[index];
                }
            }
        });

        main_vp_container.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setChecked(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        mFragmentManager = getSupportFragmentManager();
        mFragmentList = new ArrayList<>();

        NewsFragment newsFragment = new NewsFragment();
        MeetingFragment meetingFragment = new MeetingFragment();
        TranslationFragment translationFragment = new TranslationFragment();
        MeFragment meFragment = new MeFragment();

        mFragmentList.add(newsFragment);
        mFragmentList.add(meetingFragment);
        mFragmentList.add(translationFragment);
        mFragmentList.add(meFragment);

        FragmentViewPagerAdapter adapter = new FragmentViewPagerAdapter(getSupportFragmentManager(), mFragmentList);
        main_vp_container.setAdapter(adapter);
    }

    private void initData() {
        mPreCheckedId = mCheckIds[0];
        main_vp_container.setCurrentItem(0);
        main_rg_radio.check(mCheckIds[0]);
    }


    public void setChecked(int position) {
        mFragmentIndex = position;
        main_rg_radio.check(mCheckIds[position]);
        main_tb_tv_title.setText(mTabs[position]);
        if (position == 2) {
            main_tb_rl_title.setVisibility(View.VISIBLE);
            main_tb_tv_title.setVisibility(View.GONE);
        } else {
            main_tb_rl_title.setVisibility(View.GONE);
            main_tb_tv_title.setVisibility(View.VISIBLE);
        }
    }
}
